﻿using ShareFrame.Business.Base_Manage;
using ShareFrame.Entity;
using ShareFrame.Util;
using Microsoft.AspNetCore.Mvc;
using NSwag.Annotations;
using System.Collections.Generic;
using System.Threading.Tasks;
using ShareFrame.IBusiness.Base_Manage;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using ShareFrame.Entity.Base_Manage;
using ShareFrame.Entity.DTO;
using Microsoft.Extensions.Options;

namespace ShareFrame.Api.Controllers.Base_Manage
{
    /// <summary>
    /// 系统用户管理控制器
    /// </summary>
    [Route("/Base_Manage/[controller]/[action]")]
    [OpenApiTag("系统用户管理接口", Description = "用于系统用户表的相关增删改查操作")]
    [ApiExplorerSettings(GroupName = "系统管理")]
    public class Base_UserController : BaseApiController
    {
        #region DI

        public Base_UserController
            (
            IBase_UserBusiness userBus,
            IConfiguration configuration,
            IBase_UploadTypeBusiness base_UploadTypeBus,
            IBase_UploadRecordsBusiness base_UploadRecordsBus,
            IOptions<BaseDbOption> basedbOptions

            )
        {
            _userBus = userBus;
            _configuration = configuration;
            _basedbOptions = basedbOptions.Value;
            _base_UploadTypeBus = base_UploadTypeBus;
            _base_UploadRecordsBus = base_UploadRecordsBus;
        }

        private readonly IConfiguration _configuration;
        private readonly BaseDbOption _basedbOptions;
        IBase_UserBusiness _userBus { get; }
        IBase_UploadTypeBusiness _base_UploadTypeBus { get; }
        IBase_UploadRecordsBusiness _base_UploadRecordsBus { get; }

        #endregion

        #region 获取
        /// <summary>
        /// 获取实体表中全部信息
        /// </summary>
        /// <param name="input">根据传入的参数查询并分页</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<Base_UserDTO>> GetDataList(PageInput<Base_UsersInputDTO> input)
        {
            return await _userBus.GetDataListAsync(input);
        }

        /// <summary>
        /// 获取指定实体信息
        /// </summary>
        /// <param name="input">传入的指定参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Base_UserDTO> GetTheData(IdInputDTO input)
        {
            return await _userBus.GetTheDataAsync(input.id) ?? new Base_UserDTO();
        }
        /// <summary>
        /// 构建前端Select远程搜索数据
        /// </summary>
        /// <param name="input">查询参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<SelectOption>> GetOptionList(OptionListInputDTO input)
        {
            return await _userBus.GetOptionListAsync(input);
        }

        #endregion

        #region 提交
        /// <summary>
        /// 保存提交非空验证调用对应方法
        /// </summary>
        /// <param name="input">传入待操作的用户信息</param>
        /// <returns></returns>
        [HttpPost]
        public async Task SaveData(UserEditInputDTO input)
        {
            if (!input.newPwd.IsNullOrEmpty())
                input.Password = input.newPwd.ToMD5String();
            if (input.Id.IsNullOrEmpty())
            {
                InitEntity(input);

                await AddData(input);
            }
            else
            {
                await UpdateData(input);
            }
        }

        /// <summary>
        /// 基础添加方法
        /// </summary>
        /// <param name="data">待添加的实体信息</param>
        /// <returns></returns>
        [HttpPost]
        [ApiPermission("Base_User.Add")]
        public async Task AddData(UserEditInputDTO data)
        {
            await _userBus.AddDataAsync(data);
        }

        /// <summary>
        /// 基础更新方法
        /// </summary>
        /// <param name="data">待更新的实体信息</param>
        /// <returns></returns>
        [HttpPost]
        [ApiPermission("Base_User.Edit")]
        public async Task UpdateData(UserEditInputDTO data)
        {
            await _userBus.UpdateDataAsync(data);
        }

        /// <summary>
        /// 根据传入的条件组 更新对应的值
        /// </summary>
        /// <param name="input">传入的条件信息</param>
        /// <returns></returns>
        [HttpPost]
        [ApiPermission("Base_User.Edit")]
        public async Task UpdateDataByConditions(Base_UserUpdateUserAvatar input)
        {
            await _userBus.UpdateDataAsyncByConditions(_basedbOptions, input);
        }


        /// <summary>
        /// 基础删除方法
        /// </summary>
        /// <param name="ids">传入指定删除条件进行单项或多项删除</param>
        /// <returns></returns>
        [HttpPost]
        [ApiPermission("Base_User.Delete")]
        public async Task DeleteData(List<string> ids)
        {
            await _userBus.DeleteDataAsync(ids);
        }

        /// <summary>
        /// 上传头像文件
        /// </summary>
        /// <param name="file">文件对象</param>
        /// <returns></returns>
        [HttpPost]
        [ApiPermission("Base_User.Edit")]
        public async Task<IActionResult> UploadFileByForm(IFormFile file)
        {
            Base_UploadType uploadType = await _base_UploadTypeBus.GetTheDataAsync(_basedbOptions, "Base_User");
            IActionResult result = UploadFileBase(file, _configuration, uploadType.FolderName);
            JObject jo = (JObject)JsonConvert.DeserializeObject(result.ToJson());
            jo = (JObject)JsonConvert.DeserializeObject(jo["Content"].ToString());
            if (jo["status"].ToString().Equals("done"))
            {
                Base_UploadRecords bur = new Base_UploadRecords();
                bur.TypeName = "头像文件";
                bur.FileURL = jo["url"].ToString();
                bur.Remark = "上传头像文件";
                InitEntity(bur);
                await _base_UploadRecordsBus.AddDataAsync(bur);
            }
            return result;
        }

        #endregion
    }
}